package thread;

import java.util.Collection;
import java.util.Iterator;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;

class MyThreadPool{
    private BlockingQueue<Runnable> queue = null;

    public MyThreadPool(int n){
        queue = new ArrayBlockingQueue<Runnable>(1000);

        for(int i =0;i<n;i++) {
            Thread t1 = new Thread(() -> {
                while(true){
                    try {
                        Runnable runnable = queue.take();
                        runnable.run();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
            t1.start();
        }
    }

    public void submit(Runnable runnable) throws InterruptedException {
        queue.put(runnable);
    }
}

public class Demo26 {
    public static void main(String[] args) throws InterruptedException {
        MyThreadPool threadPool = new MyThreadPool(10);

        for (int i = 0; i <1000 ; i++) {
            int id = i;//这里为什么不能写在submit()里面或者for循环外面
            threadPool.submit(()->{
                System.out.println(Thread.currentThread().getName()+"打印id="+id);
            });
        }
    }
}
